package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/save")
    public String save() {
        System.out.println("UserController is run ...");
        return "success.jsp";
    }

    @RequestMapping("/image")
    public String show() {
        System.out.println("image show ...");
        return "springmvc.jsp";
    }

    @RequestMapping("/requestParam1")
    public String requestParam(String name, Integer age) {
        System.out.println("requestParam is run ..." + name + ", " + age);
        return "success.jsp";
    }

    @RequestMapping("/requestParam2")
    public String requestParam2(@RequestParam(value = "username", required = true) String name) {
        System.out.println("requestParam2 is run  " + name);
        return "success.jsp";
    }

    @RequestMapping("/requestParam3")
    public String requestParam3(User user) {
        System.out.println("requestParam3 is run ... " + user);
        return "success.jsp";
    }

    //当方法参数中具有POJO类型参数与普通类型参数时，URL地址传入的参数不仅给POJO对象属性赋值，也给方法的普通类型参数赋值
    //此时可以使用@RequestParam注解中的value属性给参数取名
    @RequestMapping("/requestParam4")
    public String requestParam4(User user, @RequestParam(value = "ageAge") Integer age) {
        System.out.println("requestParam4 is run " + user + ", " + age);
        return "success.jsp";
    }

    //http://localhost/requestParam5?address.city=hefei
    @RequestMapping("/requestParam5")
    public String requestParam5(User user) {
        System.out.println(user.getAddress().getCity());
        return "success.jsp";
    }

    //http://localhost/requestParam6?addresses[0].city=hefei&addresses[1].province=anhui
    @RequestMapping("/requestParam6")
    public String requestParam6(User user) {
        System.out.println(user.getAddresses());
        return "success.jsp";
    }

    //http://localhost/requestParam7?nick=123&nick=234&nick=345
    @RequestMapping("/requestParam7")
    public String requestParam7(User user) {
        System.out.println(user.getNick());
        return "success.jsp";
    }

    //http://localhost/requestParam8?addressMap['job'].city=beijing&addressMap['home'].province=henan
    @RequestMapping("/requestParam8")
    public String requestParam8(User user) {
        System.out.println(user.getAddressMap());
        return "success.jsp";
    }

    //http://localhost/requestParam9?nick=itheima&nick=itcast
    @RequestMapping("/requestParam9")
    public String requestParam9(String[] nick) {
        System.out.println(nick[0] + ", " + nick[1]);
        return "success.jsp";
    }

    //springmvc默认将list作为对象处理,赋值前先创建对象,然后将nick作为对象的属性进行处理,但list是接口,无法创建对象
    //此时考虑用ArrayList但将ArrayList作为对象处理,其并没有nick属性,因此获取不到数据,,所以需要告知springmvc的处理器你nick是一组数据
    //通过@RequestParam注解即可
    @RequestMapping("/requestParam10")
    public String requestParam10(@RequestParam("nick") List<String> nick) {
        System.out.println(nick);
        return "success.jsp";
    }

    //http://localhost/requestParam11?date=2012-11-11
    @RequestMapping("/requestParam11")
    public String requestParam11(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        System.out.println(date);
        //return "/success.jsp";
        //最终返回的页面如果未设定绝对访问路径，将从类映射地址所在目录中查找
        return "success.jsp";
    }

}
